package com.seele.tigerwallet.ui.activtiy;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.ui.base.TitleBaseActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 确认助记词界面
 */
public class ConfirmMnemonicActivity extends TitleBaseActivity {

    protected static final String TAG = ConfirmMnemonicActivity.class.getSimpleName();
    private List<String> mNormalStrings = new ArrayList<>();//默认现实的助记词
    private List<String> mBundleStrings;//传值过来的List;
    private List<String> mSelectStrings = new ArrayList<>(12);//选择的助记词
    private TextView[] mTvMnemonicShow = new TextView[12];
    private TextView[] mTvMnemonicInput = new TextView[12];


    @Override
    public int getContainerViewId() {
        return R.layout.activity_confirm_mnemonic;
    }

    @Override
    protected void initView() {
        mBackImg.setVisibility(View.GONE);
        setTitle(getResources().getString(R.string.confirm_mnemonic));

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            mBundleStrings = bundle.getStringArrayList(TAG);
            mNormalStrings.addAll(mBundleStrings);
            //打乱List顺序
            Collections.shuffle(mNormalStrings);
            onRecord();
        }
    }


    //设置助记词
    private void onRecord() {
        for (int i = 0; i < 12; i++) {
            int textViewId = getResources().getIdentifier("tv_show" + i, "id", ConfirmMnemonicActivity.this.getPackageName());
            mTvMnemonicShow[i] = (TextView) findViewById(textViewId);
        }
        for (int i = 0; i < 12; i++) {
            int textViewId = getResources().getIdentifier("tv_input" + i, "id", ConfirmMnemonicActivity.this.getPackageName());
            mTvMnemonicInput[i] = (TextView) findViewById(textViewId);
            Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/CENTURY_GOTHIC.TTF");
            mTvMnemonicInput[i].setText(mNormalStrings.get(i));
        }
    }

    public void onTextClick(View v) {
        TextView textView = (TextView) ((LinearLayout) v).getChildAt(0);
        if (v.getTag() == null || v.getTag().equals("0")) { // 未选中被点击
            v.setTag("1");
            v.setBackgroundResource(R.drawable.mnemonic_show_select_shape);
            mSelectStrings.add(textView.getText().toString());
            if (mSelectStrings.size() == 12) {
                boolean record = true;
                for (int i = 0; i < mSelectStrings.size(); i++) {
                    if (!mSelectStrings.get(i).equals(mBundleStrings.get(i))) {
                        record = false;
                        break;
                    }
                }
                if (record) {
                    Toast.makeText(ConfirmMnemonicActivity.this, getResources().getString(R.string.mnemonic_correctness), Toast.LENGTH_LONG).show();
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList(KeyActivity.TAG, (ArrayList<String>) mBundleStrings);
                    startActivity(KeyActivity.class, bundle);
                    finish();
                } else {
                    showAlertDialog(getResources().getString(R.string.mnemonic_error), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            clearMnemonic();
                        }
                    });
                }
            }

        } else if (v.getTag().equals("1")) { // 选中被点击
            v.setTag("0");
            v.setBackgroundColor(Color.WHITE);
            for (int i = 0; i < mSelectStrings.size(); i++) {
                if (mSelectStrings.get(i).equals(textView.getText().toString())) {
                    mSelectStrings.remove(i);
                    break;
                }
            }
        }
        Log.i(TAG, "text:" + textView.getText());

        for (int i = 0; i < 12; i++) {
            mTvMnemonicShow[i].setText("");
        }
        for (int i = 0; i < mSelectStrings.size(); i++) {
            mTvMnemonicShow[i].setText(mSelectStrings.get(i));
        }
    }

    private void clearMnemonic() {
        mSelectStrings.clear();
        for (int i = 0; i < mTvMnemonicShow.length; i++) {
            mTvMnemonicShow[i].setText("");
        }
        LinearLayout[] linearLayouts = new LinearLayout[12];
        for (int i = 0; i < 12; i++) {
            int textViewId = getResources().getIdentifier("layout_input" + i, "id", ConfirmMnemonicActivity.this.getPackageName());
            linearLayouts[i] = (LinearLayout) findViewById(textViewId);
            linearLayouts[i].setBackgroundColor(Color.WHITE);
            linearLayouts[i].setTag("0");
        }
    }

}
